<%@page import="javax.naming.Context"%>
<%@page import="java.util.Map.Entry"%>
<%@page import="javax.naming.InitialContext"%>
<%@page import="java.util.Date"%>
<%@page import="java.io.File"%>
<%@page import="java.lang.management.*"%>
<%@page contentType="text/html; charset=GBK" pageEncoding="GBK"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=GBK">
        <title>JVM Monitor</title>
    </head>
    <body>
   	<b>********** System **********</b><br><br> 
   	System Everage Load:<%=ManagementFactory.getOperatingSystemMXBean().getSystemLoadAverage()%><br><br>
    <hr>
<%
    int mb = 1024*1024;

    //Getting the runtime reference from system
    Runtime runtime = Runtime.getRuntime();
%>
    <b>********** Heap utilization statistics [MB] **********</b><br><br>
	Used Memory:<%=(runtime.totalMemory() - runtime.freeMemory()) / mb%><br>
    Free Memory:<%=runtime.freeMemory() / mb%><br>
    Total Memory:<%=runtime.totalMemory() / mb%><br>
    Max Memory:<%=runtime.maxMemory() / mb%><br><br>
    <hr>    
    
	<b>********** GC statistics **********</b><br><br>
<%
/*
Young GC names:
// Oracle (Sun) HotSpot   
    // -XX:+UseSerialGC   
    'Copy',   
    // -XX:+UseParNewGC   
    'ParNew',   
    // -XX:+UseParallelGC   
    'PS Scavenge',   
       
    // Oracle (BEA) JRockit   
    // -XgcPrio:pausetime   
    'Garbage collection optimized for short pausetimes Young Collector',   
    // -XgcPrio:throughput   
    'Garbage collection optimized for throughput Young Collector',   
    // -XgcPrio:deterministic   
    'Garbage collection optimized for deterministic pausetimes Young Collector'
 
Old GC Names
// Oracle (Sun) HotSpot   
    // -XX:+UseSerialGC   
    'MarkSweepCompact',   
    // -XX:+UseParallelGC and (-XX:+UseParallelOldGC or -XX:+UseParallelOldGCCompacting)   
    'PS MarkSweep',   
    // -XX:+UseConcMarkSweepGC   
    'ConcurrentMarkSweep',   
       
    // Oracle (BEA) JRockit   
    // -XgcPrio:pausetime   
    'Garbage collection optimized for short pausetimes Old Collector',   
    // -XgcPrio:throughput   
    'Garbage collection optimized for throughput Old Collector',   
    // -XgcPrio:deterministic   
    'Garbage collection optimized for deterministic pausetimes Old Collector'    
*/
    long totalMinorGcCount = 0;
    long totalMinorGcTime = 0;
    long totalFullGcCount = 0;
    long totalFullGcTime = 0;
    for(GarbageCollectorMXBean gc :
            ManagementFactory.getGarbageCollectorMXBeans()) {
        System.out.println(gc.getName());
        long count = gc.getCollectionCount(); 
        long time = gc.getCollectionTime();
        if(gc.getName().contains("Young") || 
                gc.getName().contains("Copy") ||
                gc.getName().contains("ParNew") ||
                gc.getName().contains("PS Scavenge")){
            totalMinorGcCount += count>0?count:0;
            totalMinorGcTime += time>0?time:0;
        }else{
            totalFullGcCount += count>0?count:0;
            totalFullGcTime += time>0?time:0;
        }

    }
%>    
YGC:<%=totalMinorGcCount%> YGCT(ms):<%=totalMinorGcTime%><br>
FGC:<%=totalFullGcCount%> FGCT(ms):<%=totalFullGcTime%><br><br>
		
<%
StringBuilder ss = new StringBuilder();
   for(MemoryPoolMXBean pb :
            ManagementFactory.getMemoryPoolMXBeans()) {
            if(pb == null) continue;
            try{
            	ss.append(pb.getName()).append(" ");
                ss.append(pb.getType().name()).append(" ");
                ss.append(pb.getUsage().getUsed()*100/pb.getUsage().getMax());
                ss.append("%<br> ");	
            }catch(java.lang.InternalError e){
            	ss.append(e.getMessage());
            	ss.append("<br> ");	
            }
            
    }
%>    
<%=ss.toString()%><br><hr>

<%
ThreadMXBean tb = ManagementFactory.getThreadMXBean();
long[] deadIds = tb.findDeadlockedThreads();
int deadThreadCount = (deadIds==null?0:deadIds.length);
StringBuilder dts = new StringBuilder();
if(deadThreadCount > 0){
    for(long id:deadIds){
        dts.append(id).append(",");
    }
}
%>
<b>********** <a href="threads.jsp" target="_blank">Threads</a> **********</b><br><br>
Total Threads:<%=tb.getThreadCount()%><br>
Dead Lock Threads(<%=deadThreadCount%>):<%=dts.toString()%><br><br>

<hr>

<%
File[] roots = File.listRoots();
    StringBuilder sb = new StringBuilder();

    /* For each filesystem root, print some info */
    for (File root : roots) {
        sb.append("File system root: ");
        sb.append(root.getAbsolutePath());
        sb.append("<br/>");
        sb.append("Total space (mb): ");
        sb.append(root.getTotalSpace()/mb);
        sb.append("<br/>");
        sb.append("Free space (mb): ");
        sb.append(root.getFreeSpace()/mb);
        sb.append("<br/>");
        sb.append("Usable space (mb): ");
        sb.append(root.getUsableSpace()/mb);
        sb.append("<br/>");
    }
%>       

    <b>********** Disk Space **********</b><br><br>
    <%=sb.toString()%><br><br>
    
<br/>
                
                
    </body>
</html>
